<?php
namespace app\api\controller;
use app\common\model\User;
use app\common\model\ShopConfig;
use app\api\logic\RechargeLogic;
use app\api\validate\Recharge as RechargeValidate;
use think\Db;
use Exception;
use think\Request;
use think\response\Json;
use tools\Crypt;
use tools\DySms;
use think\facade\Cache;
use think\facade\Env;

class RechargeController extends ApiBase
{
    protected $authExcept = [
        'rechargeTemplate'
    ];
    public function rechargeTemplate(Request $request)
    {
        $list = RechargeLogic::getTemplate();
        return success($list);
    }

    /**
     * note 充值
     */
    public function recharge(){
        $post = $this->request->post();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        if($post['template_id']){
            $remplate = Db::name('recharge_template')
                        ->where(['id'=>$post['template_id'],'delete_time'=>0])
                        ->find();
            
            if(empty($remplate)){
                return error('该充值模板不存在');
            }
        }else{
            if(!$post['money']){
                return error('请输入充值金额');
            }
            $min_money = ShopConfig::get('recharge', 'min_money');
            if($post['money'] < $min_money){
                return error('最低充值金额为'.$min_money);
            }
        }
        $data = RechargeLogic::recharge($user_id,$post);
        if($data){
            return success($data,'');
        }
        return error('信息获取错误');
    }

    /**
     * 充值记录
     */
    public function rechargeRecord()
    {
        $get = $this->request->get();
        $user_id =$this->uid;
        if(!$user_id){
            return unauthorized('请先授权');
        }
        $list = RechargeLogic::rechargeRecord($user_id,$get,$this->page,$this->limit);
        return success($list);
    }
}